<template>
  <div class="report-view">
    <h1>报表中心</h1>
    
    <div class="filter-bar">
      <el-form :inline="true" :model="filterForm" class="filter-form">
        <el-form-item label="报表类型">
          <el-select v-model="filterForm.type" placeholder="选择报表类型">
            <el-option label="销售报表" value="sales" />
            <el-option label="库存报表" value="inventory" />
            <el-option label="客户报表" value="customer" />
            <el-option label="财务报表" value="financial" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="filterForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <div class="report-list">
      <el-table :data="reportList" border style="width: 100%">
        <el-table-column prop="id" label="报表ID" width="120" />
        <el-table-column prop="name" label="报表名称" width="200" />
        <el-table-column prop="type" label="类型" width="120">
          <template #default="scope">
            <el-tag :type="getReportTypeTag(scope.row.type)">
              {{ getReportTypeName(scope.row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="creator" label="创建人" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'completed' ? 'success' : 'warning'">
              {{ scope.row.status === 'completed' ? '已完成' : '生成中' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button 
              size="small" 
              type="primary" 
              :disabled="scope.row.status !== 'completed'"
              @click="viewReport(scope.row)"
            >
              查看
            </el-button>
            <el-button 
              size="small" 
              :disabled="scope.row.status !== 'completed'"
              @click="downloadReport(scope.row)"
            >
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next, total"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize"
          @current-change="handlePageChange"
        />
      </div>
    </div>
    
    <!-- 报表预览对话框 -->
    <el-dialog
      v-model="previewDialogVisible"
      title="报表预览"
      width="80%"
    >
      <div class="report-preview">
        <div class="preview-header">
          <h2>{{ currentReport.name }}</h2>
          <p>生成时间: {{ currentReport.createTime }}</p>
        </div>
        <div class="preview-content">
          <el-empty v-if="!currentReport.id" description="暂无报表数据" />
          <div v-else class="report-placeholder">
            <p>报表内容区域 - {{ currentReport.name }}</p>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="previewDialogVisible = false">关闭</el-button>
          <el-button type="primary" @click="downloadReport(currentReport)">
            下载报表
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 筛选表单
const filterForm = reactive({
  type: '',
  dateRange: []
})

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 报表列表数据
const reportList = ref([
  {
    id: 'RPT20230501001',
    name: '2023年4月销售汇总报表',
    type: 'sales',
    createTime: '2023-05-01 10:30:45',
    creator: '张三',
    status: 'completed'
  },
  {
    id: 'RPT20230502001',
    name: '2023年Q1季度财务报表',
    type: 'financial',
    createTime: '2023-05-02 14:25:12',
    creator: '李四',
    status: 'completed'
  },
  {
    id: 'RPT20230503001',
    name: '华东区域客户分析报表',
    type: 'customer',
    createTime: '2023-05-03 09:15:30',
    creator: '王五',
    status: 'completed'
  },
  {
    id: 'RPT20230504001',
    name: '主要产品库存预警报表',
    type: 'inventory',
    createTime: '2023-05-04 16:40:22',
    creator: '赵六',
    status: 'processing'
  }
])

// 报表预览相关
const previewDialogVisible = ref(false)
const currentReport = ref({} as any)

// 获取报表类型标签样式
const getReportTypeTag = (type: string) => {
  const typeMap: Record<string, string> = {
    'sales': 'success',
    'inventory': 'info',
    'customer': 'warning',
    'financial': 'danger'
  }
  return typeMap[type] || ''
}

// 获取报表类型名称
const getReportTypeName = (type: string) => {
  const typeMap: Record<string, string> = {
    'sales': '销售报表',
    'inventory': '库存报表',
    'customer': '客户报表',
    'financial': '财务报表'
  }
  return typeMap[type] || type
}

// 查询
const handleSearch = () => {
  ElMessage.success('查询条件已应用')
  currentPage.value = 1
  // 实际项目中这里应该调用API获取数据
}

// 重置表单
const resetForm = () => {
  filterForm.type = ''
  filterForm.dateRange = []
}

// 分页变化
const handlePageChange = (page: number) => {
  currentPage.value = page
  // 实际项目中这里应该调用API获取数据
}

// 查看报表
const viewReport = (report: any) => {
  currentReport.value = report
  previewDialogVisible.value = true
}

// 下载报表
const downloadReport = (report: any) => {
  ElMessage.success(`报表《${report.name}》开始下载`)
  // 实际项目中这里应该调用API下载报表
}
</script>

<style scoped>
.report-view {
  padding: 20px;
}

h1 {
  margin-bottom: 20px;
}

.filter-bar {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.report-list {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.report-preview {
  padding: 10px;
}

.preview-header {
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.preview-header h2 {
  margin: 0 0 10px 0;
}

.preview-content {
  min-height: 400px;
}

.report-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 400px;
  background-color: #f5f7fa;
  border: 1px dashed #dcdfe6;
  font-size: 18px;
  color: #909399;
}
</style> 